package com.huike.clues.service.impl;

import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.SysDictTypeMapper;
import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.constant.UserConstants;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.domain.entity.SysDictType;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.DictUtils;
import com.huike.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author EthanRuins
 * @date 2023/7/13
 * @apiNote
 */
@Service
@AllArgsConstructor
public class SysDictTypeServiceImpl implements ISysDictTypeService {
    private final SysDictTypeMapper sysDictTypeMapper;
    private final SysDictDataMapper sysDictDataMapper;

    @PostConstruct
    public void init() {
        List<SysDictType> dictTypeList = sysDictTypeMapper.getDictTypeAll();
        for (SysDictType dictType : dictTypeList) {
            List<SysDictData> dictData = sysDictDataMapper.select(dictType.getDictType());
            DictUtils.setDictCache(dictType.getDictType(), dictData);
        }
    }

    @Override
    public int insert(SysDictType dictType) {
        int row = sysDictTypeMapper.insert(dictType);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    @Override
    @Transactional
    public int update(SysDictType dictType) {
        SysDictType oldDict = sysDictTypeMapper.getById(dictType.getDictId());
        sysDictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
        int row = sysDictTypeMapper.update(dictType);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    @Override
    public int delete(Long[] dictIds) {
        for (Long dictId : dictIds) {
            SysDictType dictType = getById(dictId);
            if (sysDictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
                throw new CustomException(dictType.getDictName() + "已分配，不能删除");
            }
        }
        int row = sysDictTypeMapper.deleteByIds(dictIds);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    @Override
    public SysDictType getById(Long dictId) {
        return sysDictTypeMapper.getById(dictId);
    }

    @Override
    public List<SysDictType> getList(SysDictType dictType) {
        return sysDictTypeMapper.getDictTypeList(dictType);
    }

    @Override
    public List<SysDictType> getDictTypeAll() {
        return sysDictTypeMapper.getDictTypeAll();
    }

    @Override
    public String checkDictTypeUnique(SysDictType dictType) {
        long dictId = StringUtils.isNull(dictType.getDictId()) ? -1L : dictType.getDictId();
        SysDictType sysDictType = sysDictTypeMapper.checkDictTypeUnique(dictType.getDictType());
        if (StringUtils.isNotNull(sysDictType) && dictType.getDictId() != dictId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }
}
